﻿class ArrowFollowMouse extends MovieClip {
	function ArrowFollowMouse() {
		init();
	}
	
	function init():Void{
		createMouseInstance();
		
		for(var i:Number=0; i<100; i++){
			createArrowInstance(2 + Math.random()*5);
		}
	}
	
	function createMouseInstance():Void{
		this.createEmptyMovieClip("mouse_mc", 2);
		
		this["mouse_mc"].lineStyle(1, 0x000000, 100);
		this["mouse_mc"].beginFill(0xffcc00, 80);
		this["mouse_mc"].moveTo(-5, -5);
		this["mouse_mc"].lineTo(5, -5);
		this["mouse_mc"].lineTo(5, 5);
		this["mouse_mc"].lineTo(-5, 5);
		this["mouse_mc"].lineTo(-5, -5);
		this["mouse_mc"].endFill();
		
		this["mouse_mc"].startDrag(true);
	}
	
	function createArrowInstance(speed:Number):Void{
		var depth:Number = this.getNextHighestDepth();
		this.createEmptyMovieClip("arrow" + depth + "_mc", depth);
		var arrow_mc:MovieClip = this["arrow" + depth + "_mc"];
		
		arrow_mc._x = Math.random() * Stage.width;
		arrow_mc._y = Math.random() * Stage.height;
		
		arrow_mc.lineStyle(1, 0x000000, 100);
		arrow_mc.moveTo(0, 0);
		arrow_mc.lineTo(-10, 0);
		arrow_mc.moveTo(0, 0);
		arrow_mc.lineTo(-5, -5);
		arrow_mc.moveTo(0, 0);
		arrow_mc.lineTo(-5, 5);
		
		arrow_mc.speed= speed;
		arrow_mc.onEnterFrame = arrow_onEnterFrame;
	}
	
	function arrow_onEnterFrame():Void{
		var dx:Number = this._parent._xmouse - this._x;
		var dy:Number = this._parent._ymouse - this._y;
		var angle:Number = Math.atan2(dy, dx);
		this._rotation = angle * 180 / Math.PI;
		
		var vx:Number = Math.cos(angle) * this["speed"];
		var vy:Number = Math.sin(angle) * this["speed"];
		
		this._x += vx;
		this._y += vy;
	}
}
